package com.yegs.jltqc.U8ServiceImpl;

import com.yegs.jltqc.U8Service.QmilcodeService;
import com.yegs.jltqc.common.StoreException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.relational.core.sql.In;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QmilcodeServiceImpl implements QmilcodeService {


    
    private final static HashMap ACMap = new HashMap<Double,String>();

    static {
        ACMap.put(0.01,"IAC0_010");
        ACMap.put(0.015,"IAC0_015");
        ACMap.put(0.025,"IAC0_025");
        ACMap.put(0.04,"IAC0_040");
        ACMap.put(0.065,"IAC0_065");

        ACMap.put(0.1,"IAC0_10");
        ACMap.put(0.15,"IAC0_15");
        ACMap.put(0.25,"IAC0_25");
        ACMap.put(0.4,"IAC0_40");
        ACMap.put(0.65,"IAC0_65");

        ACMap.put(1.0,"IAC1_0");
        ACMap.put(1.5,"IAC1_5");
        ACMap.put(2.5,"IAC2_5");
        ACMap.put(4.0,"IAC4_0");
        ACMap.put(6.5,"IAC6_5");

        ACMap.put(10.0,"IAC10");
        ACMap.put(15.0,"IAC15");
        ACMap.put(25.0,"IAC25");
        ACMap.put(40.0,"IAC40");
        ACMap.put(65.0,"IAC65");

        ACMap.put(100.0,"IAC100");
        ACMap.put(150.0,"IAC150");
        ACMap.put(250.0,"IAC250");
        ACMap.put(400.0,"IAC400");
        ACMap.put(650.0,"IAC650");
        ACMap.put(1000.0,"IAC1000");
    }

    @Autowired
    @Qualifier("u8JdbcTemplate")
    private JdbcTemplate u8JdbcTemplate;

    @Override
    public List<Map<String, Object>> findAll() {
        String sql = "select * from QMILCODE";
        return u8JdbcTemplate.queryForList(sql);
    }

    @Override
    public Map<String, Object> getCodeByLevelAndQtyAndGrade(String level, Float qty, Double grade) {
        Map<String, Object> map = this.findDemoByLevelAndQty(level, qty);
        if(map ==null){
            HashMap<String,Object> hash = new HashMap<>();
            hash.put("ac",0);
            hash.put("re",1);
            return hash;
        }
        String ac = (String) ACMap.get(grade);
        HashMap<String,Object> hashMap = getValue((Integer) map.get("AUTOID"), ac);
        hashMap.put("qc",map.get("ISAMPLEQTY"));
        return hashMap;
    }
    private HashMap getValue(Integer id, String ac){
        String sql = "select top 1 * from QM_Q105E where AUTOID = ? ";
        Map<String, Object> map = u8JdbcTemplate.queryForMap(sql, id);
        if(map.get(ac).equals("↑")){
            return getValue(id - 1, ac);
        }else if(map.get(ac).equals("↓")){
            return getValue(id + 1, ac);
        }else{
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("ac",map.get(ac));
            hashMap.put("re",(Integer.parseInt((String)map.get(ac)) + 1));
            return hashMap;
        }
    }

    @Override
    public Map<String, Object> findDemoByLevelAndQty(String level, Float qty) {
        String code = null;
        String sql = "select * from QMILCODE where INMIN <= ? and ? <= INMAX" ;
        Map<String, Object> map = u8JdbcTemplate.queryForMap(sql, qty, qty);
        if(level.equals("S-1")){
            code =  (String) map.get("CCODES_1");
        }else if(level.equals("S-2")){
            code =  (String) map.get("CCODES_2");
        }else if(level.equals("S-3")){
            code =  (String) map.get("CCODES_3");
        }else if(level.equals("S-4")){
            code =  (String) map.get("CCODES_4");
        }else if(level.equals("I")){
            code =  (String) map.get("CCODE_I");
        }else if(level.equals("II")){
            code =  (String) map.get("CCODE_II");
        }else if(level.equals("III")){
            code =  (String) map.get("CCODE_III");
        }else{
            return null;
        }
        sql = "select top 1 * from QM_Q105E where CCODE = ?";
        map = u8JdbcTemplate.queryForMap(sql, code);
        return map;
    }

    private HashMap getValue(Double id,String ac,String re){
        String sql = "select * from qc_106e where AUTOID = ?";
        Map<String, Object> map = u8JdbcTemplate.queryForMap(sql, id);
        if(map.get(ac).equals("↑")){
            return getValue(id - 1, ac, re);
        }else if(map.get(ac).equals("↓")){
            return getValue(id + 1, ac, re);
        }else{
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("ac",map.get(ac));
            hashMap.put("re",map.get(re));
            return hashMap;
        }
    }

    private String get(double grade){
        String code = null;
        if(0.25 == grade){
            code = "P0025";
        }else if(0.40 == grade){
            code = "P0040";
        }else if(0.65 == grade){
            code = "P0065";
        }else if(1.0 == grade){
            code = "P0100";
        }else if(1.5 == grade){
            code = "P0150";
        }else if(2.5 == grade){
            code = "P0250";
        }else if(4.0 == grade){
            code = "P0400";
        }else if(6.5 == grade){
            code = "P0650";
        }else if(10 == grade){
            code = "P1000";
        }else if(15 == grade){
            code = "P1500";
        }else if(25 == grade){
            code = "P2500";
        }
        return code;
    }

}
